/**
*  
* @Function 生成报告任务实体类 
* @ProjectName credit-data-platform 
* @PackageName com.py.credit.entity    
* @FileName CreateReportTask.java    
* @Date 2018年10月23日上午11:20:29
* @Author Min DongXv
* @Copyright (c) 2018, 金电联行 All Rights Reserved.   
*   
*/  
package com.py.credit.entity;

import java.io.File;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.Callable;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.constant.ResourcePath;
import com.py.credit.service.BatchReportDetailService;
import com.py.credit.service.CreditReportService;
import com.py.credit.service.OutInterfaceService;
import com.py.credit.service.ShareLogService;
import com.py.credit.utils.HtmlToPdfUtils;
import com.py.framework.core.helper.HttpServletHelper;

/**
 * 
 * @Function 生成报告任务实体类
 * @ClassName CreateReportTask
 * @Date 2018年10月23日 上午11:20:29
 * @Author Min DongXv
 * 
 */
public class BatchReportTask implements Callable<CreateReportResult> {

	/**
	 * 文件路径
	 */
	private String absolutepath;
	/**
	 * pdf转换脚本路径
	 */
	private String pdfJsPath;
	/**
	 * 谷歌浏览器路径
	 */
	private String googlePath;
	/**
	 * 生成报告页面路径
	 */
	private String pdfPagePath;
	
	private String username;
	
	private String officeName;
	
	private String realName;
	
	private Long deptId;
	
	private HttpServletRequest request;
  
	private Map<String,String> map;
	
	private ShareLogService shareLogService;

	private OutInterfaceService outInterfaceService;
	
	private CreditReportService creditReportService;

	/**
	 * @param filePath
	 * @param pdfJsPath
	 * @param googlePath
	 * @param pdfPagePath
	 * @param tybm
	 * @param kId
	 * @param fileName
	 * @param batchReportDetailEntity
	 * @param batchReportDetailDao
	 */
	public BatchReportTask(String absolutepath, String pdfJsPath, String googlePath, String pdfPagePath,
			Map<String,String> map, ShareLogService shareLogService,
			OutInterfaceService outInterfaceService,CreditReportService creditReportService,String username,
			String officeName,HttpServletRequest request,String realName,Long deptId) {
		this.absolutepath = absolutepath;
		this.pdfJsPath = pdfJsPath;
		this.googlePath = googlePath;
		this.pdfPagePath = pdfPagePath;
		this.realName = realName;
		this.deptId = deptId;
		this.map = map;
		this.shareLogService = shareLogService;
		this.outInterfaceService = outInterfaceService;
		this.creditReportService = creditReportService;
		this.username = username;
		this.officeName = officeName;
		this.request = request;
	}

	/**
	 *
	 * @Function 任务执行
	 * @FileName CreateReportTask.java
	 * @MethodName call
	 * @return
	 * @throws Exception
	 * @Date 2018年10月23日 上午11:21:39
	 * @Author Min DongXv
	 *
	 */
	@Override
	public CreateReportResult call() throws Exception {
		CreateReportResult result = null;
		boolean googleConvent = googleConventSync(pdfJsPath,
				pdfPagePath + "/tybm=" + map.get("tybm") + "/kId=" + map.get("kId") + "/keyword=" + map.get("keyword"),
				absolutepath + ResourcePath.CREDIT_REPORT_DIR + map.get("tybm"), map.get("keyword")+".pdf", null, true,
				googlePath);

			shareLogService.add(2, "信用核查", HttpServletHelper.getGuestIP(request), username, realName, deptId, map.get("keyword"));
			// 将目录信息入库
			if (0 == outInterfaceService.checkIsExist(map.get("tybm"))) {
				CreditReport report = new CreditReport();
				report.setLocation(ResourcePath.CREDIT_REPORT_DIR + map.get("tybm") + "/" + map.get("keyword")+".pdf");
				report.setEntityTybm(map.get("tybm"));
				report.setEntityName(map.get("keyword"));
				report.setEntityTyshxydm(map.get("subword"));
				report.setGenerator(username);
				report.setEntityKId(map.get("kId"));
				report.setType(1);
				report.setGeneratorDept(officeName);
				creditReportService.add(report);		
	}
		return result;
	}
	
		public static boolean googleConventSync(String jsPath, String htmlUrl, String destPath, String fileName,
	            String paper, boolean displayHeaderFooter, String googlePath) {
	File file = new File(destPath);
	// 默认A4纸
	if (StringUtils.isEmpty(paper)) {
	paper = "A4";
	}
	// 如果pdf保存路径不存在，则创建路径
	if (!file.exists()) {
	file.mkdirs();
	}
	// 拼接执行参数
	StringBuilder cmd = new StringBuilder(150);
	boolean result = true;
	cmd.append("node ").append(jsPath).append(" ").append(htmlUrl).append(" ").append(destPath)
	.append(File.separator).append(fileName).append(" ").append(paper).append(" ")
	.append(String.valueOf(displayHeaderFooter));
	if (StringUtils.isNotBlank(googlePath)) {
	cmd.append(" ").append(googlePath);
	}
	ExceptionLogger.error("HtmlToPdfUtils#googleConventSync",
	"打印html生成pdf命令:" + cmd.toString());
	InputStream is = null;
	try {
	Process process = Runtime.getRuntime().exec(cmd.toString());
	is = process.getInputStream();
	if (null != is) {
	is.close();
	}
	if (0 != process.waitFor()) {
	result = false;
	}
	return result;
	} catch (Exception e) {
	ExceptionLogger.error("HtmlToPdfUtils#googleConventSync",
	"主产品模块PDF生成失败！" + "命令:" + cmd.toString() + " 错误原因" + e.getMessage());
	result = false;
	return result;
	}
	}
}